package com.darren.befriend.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.darren.befriend.BaseActivity;
import com.darren.befriend.R;
import com.darren.befriend.UserSettingInitialActivity;
import com.parse.ParseObject;
import com.parse.ParseUser;

public class AboutMeFragment extends Fragment {

	private TextView mAboutMeRemain;
	private ParseUser mUser;
	private EditText mAboutMe;

	public static Fragment create() {

		AboutMeFragment fragment = new AboutMeFragment();
		return fragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		ViewGroup view = (ViewGroup) inflater.inflate(R.layout.layout_about_me,
				container, false);
		mAboutMe = (EditText) view.findViewById(R.id.about_me_edt);
		mAboutMeRemain = (TextView) view.findViewById(R.id.about_remain);
		mAboutMeRemain.setText(String.format(
				getString(R.string.about_me_remain), 2500));
		mAboutMe.addTextChangedListener(aboutWatcher);
		mUser = ((BaseActivity) getActivity()).getCurrentUser();
		ParseObject meInfo = (ParseObject) mUser.get("MeInfo");
		String about = meInfo.getString("About");
		if (!about.trim().equals("")) {

			mAboutMe.setText(about);
			mAboutMeRemain
					.setText(String.format(getString(R.string.about_me_remain),
							2500 - about.length()));
		}

		return view;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see android.support.v4.app.Fragment#onPause()
	 */
	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();

		String aboutMe = mAboutMe.getText().toString();
		ParseObject meInfo = (ParseObject) mUser.get("MeInfo");
		meInfo.put("About", aboutMe);
		mUser.put("MeInfo", meInfo);
		mUser.saveInBackground();
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		if (getActivity() instanceof UserSettingInitialActivity) {

			((UserSettingInitialActivity) getActivity())
					.setActivityTitle(getString(R.string.about));
			((UserSettingInitialActivity) getActivity()).setNavigationImage(
					R.drawable.item_back, Integer.MAX_VALUE);
		}
	}

	TextWatcher aboutWatcher = new TextWatcher() {

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
		}

		@Override
		public void afterTextChanged(Editable s) {

			mAboutMeRemain.setText(String.format(
					getString(R.string.about_me_remain), 2500 - s.length()));
		}
	};
}
